﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.Plumbing
{
	public partial class FrmCisternCalculate : System.Windows.Forms.Form
	{
		public FrmCisternCalculate(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
			this.userSetting = new HVACUserSetting();
			//base.Icon = Resource.YArchitechIcon;
		}

		private void btnSupple_Click(object sender, EventArgs e)
		{
			FrmSupplyWaterCal frmSupplyWaterCal = new FrmSupplyWaterCal(this.doc);
			if (frmSupplyWaterCal.ShowDialog() == DialogResult.OK)
			{
				this.txtQg.Text = frmSupplyWaterCal.Qg.ToString();
			}
		}

		private void InitialInterfaceInfo()
		{
			IList<string> cisternCal = this.userSetting.GetCisternCal();
			this.txtQg.Text = cisternCal[0];
			this.txtQb.Text = cisternCal[1];
			this.txtTb.Text = cisternCal[2];
			this.txtTt.Text = cisternCal[3];
			this.txtTx.Text = cisternCal[4];
			this.txtIndoorFlow.Text = cisternCal[5];
			this.txtOutdoorFlow.Text = cisternCal[6];
			this.txtSpray.Text = cisternCal[7];
		}

		private void SaveInterfaceInfo()
		{
			IList<string> list = new List<string>();
			list.Add(this.txtQg.Text);
			list.Add(this.txtQb.Text);
			list.Add(this.txtTb.Text);
			list.Add(this.txtTt.Text);
			list.Add(this.txtTx.Text);
			list.Add(this.txtIndoorFlow.Text);
			list.Add(this.txtOutdoorFlow.Text);
			list.Add(this.txtSpray.Text);
			this.userSetting.WriteCisternCal(list);
		}

		private double CalVolume()
		{
			double num = Convert.ToDouble(this.txtQb.Text);
			double num2 = Convert.ToDouble(this.txtQg.Text);
			double num3 = Convert.ToDouble(this.txtTb.Text);
			return Convert.ToDouble(StringTools.CutOffEfficacious(((num - num2) * num3).ToString(), 2));
		}

		private double CalWaterUse()
		{
			double num = Convert.ToDouble(this.txtTx.Text);
			double num2 = this.UnitTransfor(Convert.ToDouble(this.txtIndoorFlow.Text));
			double num3 = this.UnitTransfor(Convert.ToDouble(this.txtOutdoorFlow.Text));
			double num4 = this.UnitTransfor(Convert.ToDouble(this.txtSpray.Text));
			double num5 = Convert.ToDouble(this.txtQg.Text);
			return Convert.ToDouble(StringTools.CutOffEfficacious((num * (num2 + num3) + num4 - num * num5).ToString(), 2));
		}

		private double CalTotalVolume()
		{
			double num = Convert.ToDouble(this.txtVx.Text);
			double num2 = Convert.ToDouble(this.txtVl.Text);
			return num + num2;
		}

		private double UnitTransfor(double LPerSecond)
		{
			return LPerSecond * 3.6;
		}

		private bool CheckTextValue()
		{
			double num = Convert.ToDouble(this.txtQg.Text);
			double num2 = Convert.ToDouble(this.txtQb.Text);
			double num3 = Convert.ToDouble(this.txtTb.Text);
			double num4 = Convert.ToDouble(this.txtTt.Text);
			double num5 = Convert.ToDouble(this.txtTx.Text);
			double num6 = Convert.ToDouble(this.txtIndoorFlow.Text);
			double num7 = Convert.ToDouble(this.txtOutdoorFlow.Text);
			double num8 = Convert.ToDouble(this.txtSpray.Text);
			if (num <= 0.0)
			{
				YJKMessageBox.Show("补充水量值应该大于0！");
				this.txtQg.Select();
				this.txtQg.Focus();
				return false;
			}
			if (num2 <= 0.0)
			{
				YJKMessageBox.Show("水泵出水量应该大于0！");
				this.txtQb.Select();
				this.txtQb.Focus();
				return false;
			}
			if (num3 <= 0.0)
			{
				YJKMessageBox.Show("水泵运行时间应该大于0！");
				this.txtTb.Select();
				this.txtTb.Focus();
				return false;
			}
			if (num4 <= 0.0)
			{
				YJKMessageBox.Show("水泵运行间隔应该大于0！");
				this.txtTt.Select();
				this.txtTt.Focus();
				return false;
			}
			if (num5 <= 0.0)
			{
				YJKMessageBox.Show("火灾延续时间应该大于0！");
				this.txtTx.Select();
				this.txtTx.Focus();
				return false;
			}
			if (num6 <= 0.0)
			{
				YJKMessageBox.Show("室内消火栓水量应该大于0！");
				this.txtIndoorFlow.Select();
				this.txtIndoorFlow.Focus();
				return false;
			}
			if (num7 <= 0.0)
			{
				YJKMessageBox.Show("室外消火栓水量应该大于0！");
				this.txtOutdoorFlow.Select();
				this.txtOutdoorFlow.Focus();
				return false;
			}
			if (num8 <= 0.0)
			{
				YJKMessageBox.Show("自动喷洒水量应该大于0！");
				this.txtSpray.Select();
				this.txtSpray.Focus();
				return false;
			}
			return true;
		}

		private void FrmCisternCalculate_Load(object sender, EventArgs e)
		{
			this.InitialInterfaceInfo();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (!this.CheckTextValue())
			{
				return;
			}
			if (!this.CheckQb())
			{
				return;
			}
			double num = this.CalVolume();
			this.txtVl.Text = num.ToString();
			double num2 = this.CalWaterUse();
			this.txtVx.Text = num2.ToString();
			double num3 = this.CalTotalVolume();
			this.txtVy.Text = num3.ToString();
			if (this.CheckInWaterRight())
			{
				this.SaveInterfaceInfo();
			}
		}

		private bool CheckQb()
		{
			double num = Convert.ToDouble(this.txtQb.Text);
			double num2 = Convert.ToDouble(this.txtQg.Text);
			if (num < num2)
			{
				YJKMessageBox.Information("水泵出水量不应小于补水量！");
				return false;
			}
			return true;
		}

		private bool CheckInWaterRight()
		{
			double num = Convert.ToDouble(this.txtVl.Text);
			double num2 = Convert.ToDouble(this.txtQg.Text);
			double num3 = Convert.ToDouble(this.txtTt.Text);
			if (num > num2 * num3)
			{
				YJKMessageBox.Information("进水管太小！\n必须满足:调节容积<水源供水能力×运行间隔时间！");
				return false;
			}
			return true;
		}

		private Document doc;

		private HVACUserSetting userSetting;
	}
}
